import os
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
from sklearn.model_selection import train_test_split
import itertools
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Flatten, Dropout, concatenate, Input, Conv2D, MaxPooling2D
from keras.optimizers import Adam, Adadelta
from keras.layers.advanced_activations import LeakyReLU
from keras.utils.np_utils import to_categorical
import glob
from sklearn.metrics import classification_report
!pip install tflearn
import tflearn.datasets.oxflower17 as oxflower17
img_height=100
img_width=100
batch_size=32
nb_epochs=25
#Using data present in local folder
def get_immediate_subdirectories(a_dir):
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
specPath='F:\\GreatLearning\AI\\ComputerVision\\Project\\Flowers-Classification\\17flowers-train\\jpg'
specPathTest='F:\\GreatLearning\\AI\\ComputerVision\\Project\\Flowers-Classification\\Test\\'
cat_Folder_list=get_immediate_subdirectories(specPath)
Flower_species=cat_Folder_list
print('List of Flower species: ', Flower_species)
#No. of images under each plant species foler for train
for img in Flower_species:
print('{} --> {} training images'.format(img, len(os.listdir(os.path.join(specPath, img)))))
import glob
rootdir='F:\\GreatLearning\\AI\\ComputerVision\\Project\\'
rootJPG=os.path.join(os.path.join(rootdir,'Flowers-Classification\\17flowers-train'),'jpg')
os.chdir(os.path.join(specPath,Flower_species[0])) #changing current directory to open file easily
count=0
imgList=[]
for file in glob.glob("*.jpg"):
print(file)
count+=1
imgList.append(file)
if (count==10):
break
from PIL import Image
Image.open("image_0002.jpg")
import imageio
import matplotlib.pyplot as plt
%matplotlib inline
#capture basic details of images
def imgBasics(path,imgName):
img1= os.path.join(path, imgName)
pic = imageio.imread(img1)
plt.figure(figsize = (5,5))
plt.imshow(pic)
#Basic properties of image
print('Type of the image : ' , type(pic))
print('Shape of the image : {}'.format(pic.shape))
print('Image Hight {}'.format(pic.shape[0]))
print('Image Width {}'.format(pic.shape[1]))
print('Dimension of Image {}'.format(pic.ndim))
print('Image size {}'.format(pic.size))
print('Maximum RGB value in this image {}'.format(pic.max()))
print('Minimum RGB value in this image {}'.format(pic.min()))
print('Value of only R channel {}'.format(pic[ 100, 50, 0]))
print('Value of only G channel {}'.format(pic[ 100, 50, 1]))
print('Value of only B channel {}'.format(pic[ 100, 50, 2]))
firstSpecies=os.path.join(specPath,Flower_species[0])
imgBasics(firstSpecies,imgList[0])
imgBasics(firstSpecies,imgList[1])
imgBasics(firstSpecies,imgList[9])
imgBasics(firstSpecies,imgList[5])
rootdir='F:\\GreatLearning\\AI\\ComputerVision\\Project\\'
os.chdir(rootdir) #resetting back to original directory
import glob
images_per_class = {}
for class_folder_name in os.listdir(specPath):
class_folder_path = os.path.join(specPath, class_folder_name)
if os.path.isdir(class_folder_path):
class_label = class_folder_name
images_per_class[class_label] = []
for image_path in glob.glob(os.path.join(class_folder_path, "*.jpg")):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#image_bgr = cv2.imread(image_path, cv2.IMREAD_COLOR)
images_per_class[class_label].append(img)
for key,value in images_per_class.items():
print("{0} -> {1}".format(key, len(value)))
Plot images so we can see what the input looks like
#Function for getting images class wise
def plot_for_class(label):
nb_rows = 3
nb_cols = 3
fig, axs = plt.subplots(nb_rows, nb_cols, figsize=(6, 6))
n = 0
for i in range(0, nb_rows):
for j in range(0, nb_cols):
axs[i, j].xaxis.set_ticklabels([])
axs[i, j].yaxis.set_ticklabels([])
axs[i, j].imshow(images_per_class[label][n])
n += 1
plot_for_class("0")
plot_for_class("1")
plot_for_class("6")
len(images_per_class['0'])
import random as rn
Z=images_per_class
fig,ax=plt.subplots(5,5)
fig.set_size_inches(15,15)
for i in range(5):
for j in range (5):
l=rn.randint(0,len(Z)-1)
#print(l)
k=rn.randint(0,len(Z[str(l)])-1)
#print(k)
ax[i,j].imshow(Z[str(l)][k])
ax[i,j].set_title('Flower: '+str(l))
plt.tight_layout()
Reference:
Edge Detection masks were created below
image = images_per_class["1"][8]
plt.imshow(image, cmap='gray')
# 3x3 sobel filter for horizontal edge detection
sobel_y = np.array([[ -1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
# vertical edge detection
sobel_x = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
# filter the image using filter2D(grayscale image, bit-depth, kernel)
filtered_image1 = cv2.filter2D(image, -1, sobel_x)
filtered_image2 = cv2.filter2D(image, -1, sobel_y)
f, ax = plt.subplots(1, 2, figsize=(15, 4))
ax[0].set_title('horizontal edge detection')
ax[0].imshow(filtered_image1, cmap='gray')
ax[1].set_title('vertical edge detection')
ax[1].imshow(filtered_image2, cmap='gray')
image = images_per_class["0"][8]
plt.imshow(image, cmap='gray')
# 3x3 sobel filter for horizontal edge detection
sobel_y = np.array([[ -1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
# vertical edge detection
sobel_x = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
# filter the image using filter2D(grayscale image, bit-depth, kernel)
filtered_image1 = cv2.filter2D(image, -1, sobel_x)
filtered_image2 = cv2.filter2D(image, -1, sobel_y)
f, ax = plt.subplots(1, 2, figsize=(15, 4))
ax[0].set_title('horizontal edge detection')
ax[0].imshow(filtered_image1, cmap='gray')
ax[1].set_title('vertical edge detection')
ax[1].imshow(filtered_image2, cmap='gray')
image = images_per_class["0"][8]
# 3x3 sobel filter for horizontal edge detection
sobel_y = np.array([[ 0, -1, 0],
[ -1, 5, -1],
[ 0, -1, 0]])
# vertical edge detection
sobel_x = np.array([[ 0, -1, 0],
[ -1, 5, -1],
[ 0, -1, 0]])
# filter the image using filter2D(grayscale image, bit-depth, kernel)
filtered_image1 = cv2.filter2D(image, -1, sobel_x,sobel_y)
plt.imshow(filtered_image1)
def create_mask_for_plant(image):
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
sensitivity = 35
lower_hsv = np.array([60 - sensitivity, 100, 50])
upper_hsv = np.array([60 + sensitivity, 255, 255])
mask = cv2.inRange(image_hsv, lower_hsv, upper_hsv)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
return mask
def segment_plant(image):
mask = create_mask_for_plant(image)
output = cv2.bitwise_and(image, image, mask = mask)
return output
def sharpen_image(image):
image_blurred = cv2.GaussianBlur(image, (0, 0), 3)
image_sharp = cv2.addWeighted(image, 1.5, image_blurred, -0.5, 0)
return image_sharp
image = images_per_class["9"][65]
image_mask = create_mask_for_plant(image)
image_segmented = segment_plant(image)
image_sharpen = sharpen_image(image_segmented)
fig, axs = plt.subplots(1, 4, figsize=(20, 20))
axs[0].imshow(image)
axs[1].imshow(image_mask)
axs[2].imshow(image_segmented)
axs[3].imshow(image_sharpen)
image = images_per_class["0"][65]
image_mask = create_mask_for_plant(image)
image_segmented = segment_plant(image)
image_sharpen = sharpen_image(image_segmented)
fig, axs = plt.subplots(1, 4, figsize=(20, 20))
axs[0].imshow(image)
axs[1].imshow(image_mask)
axs[2].imshow(image_segmented)
axs[3].imshow(image_sharpen)
image = images_per_class["1"][8]
image_mask = create_mask_for_plant(image)
image_segmented = segment_plant(image)
image_sharpen = sharpen_image(image_segmented)
fig, axs = plt.subplots(1, 4, figsize=(20, 20))
axs[0].imshow(image)
axs[1].imshow(image_mask)
axs[2].imshow(image_segmented)
axs[3].imshow(image_sharpen)
#we can not directly use the image, we have to process the image.
from pathlib import Path
from skimage.io import imread
from keras.preprocessing import image
import cv2 as cv
def load_image_files(container_path):
image_dir = Path(container_path)
folders = [directory for directory in image_dir.iterdir() if directory.is_dir()]
categories = [fo.name for fo in folders]
images = []
flat_data = []
target = []
count = 0
train_img = []
label_img = []
for i, direc in enumerate(folders):
for file in direc.iterdir():
count += 1
img = imread(file)
#img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img_pred = cv.resize(img, (img_height, img_width), interpolation=cv.INTER_AREA)
img_pred = image.img_to_array(img_pred)
img_pred = img_pred / 255
train_img.append(img_pred)
label_img.append(categories[i])
X = np.array(train_img)
y = np.array(label_img)
return X,y
#Using the Keras pre-processing library the image is converted to an array and then normalised.
X = []
y = []
X,y = load_image_files(specPath)
X.shape
y.shape
y[0]
plt.imshow(X[0])
plt.show()
plt.imshow(X[1],cmap='gist_earth')
plt.show()
fig=plt.figure(figsize=(15,15))
for i in range(1,101):
img=X[i]
fig.add_subplot(10,10,i)
plt.imshow(img,cmap='gray')
plt.show()
print('Label: ', y[1:101])
fig=plt.figure(figsize=(15,15))
for i in range(1,101):
img=X[1000+i]
fig.add_subplot(10,10,i)
plt.imshow(img)
plt.show()
print('Label: ', y[1001:1101])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, random_state=42, test_size=0.5)
#View data set shape
print("X_train: "+str(X_train.shape))
print("X_test: "+str(X_test.shape))
print("X_val: "+str(X_val.shape))
print("y_train: "+str(y_train.shape))
print("y_test: "+str(y_test.shape))
print("y_val: "+str(y_val.shape))
#View Raw data in train set
X_train[0]
#Reshaping Data sets for using in KNN model
from builtins import range
from builtins import object
num_training = X_train.shape[0]
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask]
num_test = X_test.shape[0]
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
num_val = X_val.shape[0]
mask = list(range(num_val))
X_val = X_val[mask]
y_val = y_val[mask]
# Reshape the image data into rows
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
X_val = np.reshape(X_val, (X_val.shape[0], -1))
print("X_train: "+str(X_train.shape))
print("X_test: "+str(X_test.shape))
print("X_val: "+str(X_val.shape))
print("y_train: "+str(y_train.shape))
print("y_test: "+str(y_test.shape))
print("y_val: "+str(y_val.shape))
print(y.view())
from sklearn.metrics import accuracy_score,confusion_matrix,recall_score,f1_score,precision_score,roc_curve,log_loss,auc
from sklearn.neighbors import KNeighborsClassifier
#KNN Model with 1 neighbour
KnnModel = KNeighborsClassifier(n_neighbors=1)
KnnModel.fit(X_train,y_train)
y_predict=KnnModel.predict(X_test)
print('Accuracy score:',accuracy_score(y_test,y_predict))
print('confuion matrix:\n',confusion_matrix(y_test,y_predict))
# Initializing the value of k and finding the accuracies on validation data
k_vals = range(1, 30, 2)
accuracies = []
for k in range(1, 30, 2):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
score = knn.score(X_val, y_val)
print("k value=%d, accuracy score=%.2f%%" % (k, score * 100))
accuracies.append(score)
# finding the value of k which has the largest accuracy
i = int(np.argmax(accuracies))
print("k=%d value has highest accuracy of %.2f%% on validation data" % (k_vals[i],accuracies[i] * 100))
knn = KNeighborsClassifier(n_neighbors=7)
knn.fit(X_train, y_train)
predictions = knn.predict(X_test)
print("EVALUATION ON TESTING DATA")
print(confusion_matrix(y_test,predictions))
print(knn.score(X_test, y_test))
plt.figure(figsize=(2,2))
plt.imshow(X_test[59].reshape(img_height,img_width,3))
plt.show()
image = X_test[59]
print('Prediction:',knn.predict(image.reshape(1, -1)))
print('Actual:',y_test[59])
plt.figure(figsize=(2,2))
plt.imshow(X_test[30].reshape(img_height,img_width,3))
plt.show()
image = X_test[30]
print('Prediction:',knn.predict(image.reshape(1, -1)))
print('Actual:',y_test[30])
predictions = knn.predict(X_test)
print(classification_report(y_test, predictions))
print(knn.score(X_test, y_test))
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, random_state=42, test_size=0.5)
#from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import OneHotEncoder
one_hot_encoder = OneHotEncoder(sparse=False)
one_hot_encoder.fit(y_train.reshape(-1, 1))
y_train = one_hot_encoder.transform(y_train.reshape(-1, 1))
#y_train = pd.DataFrame(data=y_train, columns=one_hot_encoder.categories_)
y_test = one_hot_encoder.transform(y_test.reshape(-1, 1))
#y_test = pd.DataFrame(data=y_test, columns=one_hot_encoder.categories_)
y_val = one_hot_encoder.transform(y_val.reshape(-1, 1))
#y_val = pd.DataFrame(data=y_val, columns=one_hot_encoder.categories_)
print("Shape of y_train:", y_train.shape)
print("Shape of y_test:", y_test.shape)
print("Shape of y_val:", y_val.shape)
y_train
y_test_cat = pd.DataFrame(data=y_test, columns=one_hot_encoder.categories_)
y_test_cat
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense, Activation
from tensorflow.keras import optimizers
from tensorflow.keras.layers import BatchNormalization, Dropout
model = Sequential()
model.add(Flatten())
model.add(Dense(1000,kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(500,kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(250,kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(125,kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(34,kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(17,kernel_initializer='he_normal'))
model.add(Activation('softmax'))
#updating learning rate
adam = optimizers.Adam(lr=0.009, decay=1e-6)
# Compile the model
model.compile(loss="categorical_crossentropy", metrics=["accuracy"], optimizer="adam")
# Fit the model
history=model.fit(x=X_train, y=y_train, batch_size=batch_size, epochs=nb_epochs, validation_data=(X_val, y_val))
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(X_test, y_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
Y_pred_test_cls = (model.predict(X_test) > 0.5).astype("int32")
plt.figure(figsize=(2,2))
plt.imshow(X_test[10].reshape(img_height,img_width,3))
plt.show()
print('Label - one hot encoded: \n',y_test_cat.iloc[10] )
print('Actual Label - one hot encoded: ', y_test[10])
print('Predicted Label - one hot encoded: ',Y_pred_test_cls[10] )
from tensorflow.keras.models import Sequential # initial NN
from tensorflow.keras.layers import Dense, Dropout # construct each layer
from tensorflow.keras.layers import Conv2D # swipe across the image by 1
from tensorflow.keras.layers import MaxPooling2D # swipe across by pool size
from tensorflow.keras.layers import Flatten, GlobalAveragePooling2D
from tensorflow import keras
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, random_state=42, test_size=0.5)
#from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import OneHotEncoder
one_hot_encoder = OneHotEncoder(sparse=False)
one_hot_encoder.fit(y_train.reshape(-1, 1))
y_train = one_hot_encoder.transform(y_train.reshape(-1, 1))
#y_train = pd.DataFrame(data=y_train, columns=one_hot_encoder.categories_)
y_test = one_hot_encoder.transform(y_test.reshape(-1, 1))
#y_test = pd.DataFrame(data=y_test, columns=one_hot_encoder.categories_)
y_val = one_hot_encoder.transform(y_val.reshape(-1, 1))
#y_val = pd.DataFrame(data=y_val, columns=one_hot_encoder.categories_)
print("Shape of y_train:", y_train.shape)
print("Shape of y_test:", y_test.shape)
print("Shape of y_val:", y_val.shape)
y_train
y_test_cat = pd.DataFrame(data=y_test, columns=one_hot_encoder.categories_)
y_test_cat
model = Sequential()
model.add(Conv2D(64, (5, 5), activation='relu', input_shape=(img_height, img_width, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(17, activation='softmax'))
model.summary()
# compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history=model.fit(x=X_train, y=y_train, batch_size=batch_size, epochs=nb_epochs, validation_data=(X_val, y_val))
history
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(X_test, y_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
Y_pred_test_cls = (model.predict(X_test) > 0.5).astype("int32")
plt.figure(figsize=(2,2))
plt.imshow(X_test[30].reshape(img_height,img_width,3))
plt.show()
print('Label - one hot encoded: \n',y_test_cat.iloc[30] )
print('Actual Label - one hot encoded: ', y_test[30])
print('Predicted Label - one hot encoded: ',Y_pred_test_cls[30] )
model = Sequential()
model.add(Conv2D(100, (5, 5), activation='relu', input_shape=(img_height, img_width, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=4, padding='same', activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.4))
model.add(Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(17, activation='softmax'))
model.summary()
#updating learning rate
adam = optimizers.Adam(lr=0.009, decay=1e-6)
# compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#Saving the best model using model checkpoint callback
model_checkpoint=keras.callbacks.ModelCheckpoint('Flowerspecies_CNN_model.h5', #where to save the model
save_best_only=True,
monitor='val_accuracy',
mode='max',
verbose=1)
history=model.fit(x=X_train, y=y_train,
batch_size=batch_size,
epochs=nb_epochs,
validation_data=(X_val, y_val))
#callbacks = [model_checkpoint])
history
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(X_test, y_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
Always save the model and its weights after training
model.save('./Flower_Species_Classifier_CNN.h5')
model.save_weights('./Flower_Species_Classifier_weights_CNN.h5')
datagen= keras.preprocessing.image.ImageDataGenerator(rotation_range=30,
width_shift_range=0.3,
height_shift_range=0.3,
zoom_range=[0.4,1.5],
horizontal_flip=True,
vertical_flip=True)
datagen.fit(X_train)
model = Sequential()
model.add(Conv2D(100, (5, 5), activation='relu', input_shape=(img_height, img_width, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=4, padding='same', activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.4))
model.add(Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(17, activation='softmax'))
model.summary()
#updating learning rate
adam = optimizers.Adam(lr=0.001, decay=1e-6)
# compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#Saving the best model using model checkpoint callback
model_checkpoint=keras.callbacks.ModelCheckpoint('Flowerspecies_CNN_model.h5', #where to save the model
save_best_only=True,
monitor='val_accuracy',
mode='max',
verbose=1)
history= model.fit_generator(datagen.flow(X_train, y_train, batch_size=32),
epochs=nb_epochs,
validation_data=(X_val, y_val))
#callbacks = [model_checkpoint])
history
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(X_test, y_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
model.save('./Flower_Species_Classifier_CNN_Augmented.h5')
model.save_weights('./Flower_Species_Classifier_weights_CNN_Augmented.h5')
#100 epochs
history= model.fit_generator(datagen.flow(X_train, y_train, batch_size=32),
epochs=100,
validation_data=(X_val, y_val))
#callbacks = [model_checkpoint])
history
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(X_test, y_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
model.save('./Flower_Species_Classifier_CNN_Augmented_100.h5')
model.save_weights('./Flower_Species_Classifier_weights_CNN_Augmented_100.h5')
from keras.applications import VGG16
#Load the VGG model
vgg_conv = VGG16(weights='F:/GreatLearning/AI/ComputerVision/week 2/Week 2 - CV - Mentor deck/Case study/data/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
include_top=False,
input_shape=(img_height, img_width, 3))
#we can not directly use the image, we have to process the image.
img_height=100
img_width=100
image_size=100
specPath='F:\\GreatLearning\AI\\ComputerVision\\Project\\Flowers-Classification\\17flowers-train\\jpg'
#we can not directly use the image, we have to process the image.
from pathlib import Path
from skimage.io import imread
from keras.preprocessing import image
import cv2 as cv
def load_image_files(container_path):
image_dir = Path(container_path)
folders = [directory for directory in image_dir.iterdir() if directory.is_dir()]
categories = [fo.name for fo in folders]
images = []
flat_data = []
target = []
count = 0
train_img = []
label_img = []
for i, direc in enumerate(folders):
for file in direc.iterdir():
count += 1
img = imread(file)
#img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img_pred = cv.resize(img, (img_height, img_width), interpolation=cv.INTER_AREA)
img_pred = image.img_to_array(img_pred)
#img_pred = img_pred / 255
train_img.append(img_pred)
label_img.append(categories[i])
X = np.array(train_img)
y = np.array(label_img)
return X,y
vggX = []
vggy = []
vggX,vggy = load_image_files(specPath)
vggX.shape
vggy.shape
vggy = np.asarray(vggy).reshape(vggy.shape[0],1)
vggy.shape
#from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import OneHotEncoder
one_hot_encoder = OneHotEncoder(sparse=False)
one_hot_encoder.fit(vggy.reshape(-1, 1))
vggy = one_hot_encoder.transform(vggy.reshape(-1, 1))
print("Shape of y:", vggy.shape)
vggX_train, vggX_test,vggy_train, vggy_test = train_test_split(vggX, vggy, random_state=42, test_size=0.2)
vggX_val, vggX_test, vggy_val, vggy_test = train_test_split(vggX_test, vggy_test, random_state=42, test_size=0.5)
#View data set shape
print("X_train: "+str(vggX_train.shape))
print("X_test: "+str(vggX_test.shape))
print("X_val: "+str(vggX_val.shape))
print("y_train: "+str(vggy_train.shape))
print("y_test: "+str(vggy_test.shape))
print("y_val: "+str(vggy_val.shape))
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10)) # plot 25 images
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(vggX_train[i]/255, cmap=plt.cm.binary)
plt.xlabel(vggy_train[i])
# use vgg16 pre-trained model with trainable densely connected output layer
from keras.applications import VGG16
#Load the VGG model
#Loading VGG model from mentors deck as loading online was slowing things down
#Code details captured from mentor deck
vgg_conv = VGG16(weights='F:/GreatLearning/AI/ComputerVision/week 2/Week 2 - CV - Mentor deck/Case study/data/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
include_top=False,
input_shape=(img_height, img_width, 3)
)
# Freeze all the layers except for the last layer:
for layer in vgg_conv.layers[:-4]:
layer.trainable = False
from keras import models
from keras import layers
from keras import optimizers
# Create the model
model = models.Sequential()
# Add the vgg convolutional base model
model.add(vgg_conv)
# Add new layers
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(17, activation='softmax'))
model.summary()
# image augmentation for train set and image resizing for validation & test
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
validation_datagen = ImageDataGenerator(rescale=1./255)
train_batchsize = 100
val_batchsize = 10
train_generator = train_datagen.flow(
vggX_train,vggy_train,
batch_size=train_batchsize)
validation_generator = validation_datagen.flow(
vggX_val,vggy_val,
batch_size=val_batchsize,
shuffle=False)
test_generator = validation_datagen.flow(
vggX_test,vggy_test,
batch_size=val_batchsize,
shuffle=False)
vggX_test=vggX_test/255
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(lr=2e-4),
metrics=['acc'])
history = model.fit_generator(
train_generator,
steps_per_epoch=vggX_train.shape[0]/train_generator.batch_size ,
epochs=nb_epochs,
validation_data=validation_generator,
validation_steps=vggX_val.shape[0]/validation_generator.batch_size)
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(vggX_test, vggy_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
Y_pred_test_cls = (model.predict(vggX_test) > 0.5).astype("int32")
plt.figure(figsize=(2,2))
plt.imshow(vggX_test[30]/255)
plt.show()
#print('Label - one hot encoded: \n',vggy_test_cat.iloc[30] )
print('Actual Label - one hot encoded: ', vggy_test[30])
print('Predicted Label - one hot encoded: ',Y_pred_test_cls[30] )
model.save('./Flower_Species_Classifier_VGG16_Augmented_25.h5')
model.save_weights('./Flower_Species_Classifier_weights_VGG16_Augmented_25.h5')
history = model.fit_generator(
train_generator,
steps_per_epoch=vggX_train.shape[0]/train_generator.batch_size ,
epochs=100,
validation_data=validation_generator,
validation_steps=vggX_val.shape[0]/validation_generator.batch_size)
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
plt.show()
results = model.evaluate(vggX_test, vggy_test)
print('Accuracy: %f ' % (results[1]*100))
print('Loss: %f' % results[0])
Y_pred_test_cls = (model.predict(vggX_test) > 0.5).astype("int32")
plt.figure(figsize=(2,2))
plt.imshow(vggX_test[30]/255)
plt.show()
#print('Label - one hot encoded: \n',vggy_test_cat.iloc[30] )
print('Actual Label - one hot encoded: ', vggy_test[30])
print('Predicted Label - one hot encoded: ',Y_pred_test_cls[30] )
model.save('./Flower_Species_Classifier_VGG16_Augmented_100.h5')
model.save_weights('./Flower_Species_Classifier_weights_VGG16_Augmented_100.h5')
#Keras suggests to use model.save and model.save_weights but we can pickle model using joblib as well for bigger model if usual pickling fails
from sklearn.externals import joblib
# Save the model as a pickle in a file
joblib.dump(model, 'Flower_Species_Classifier_VGG16_Augmented_100.pkl')
# Load the model from the file
model_joblib = joblib.load('Flower_Species_Classifier_VGG16_Augmented_100.pkl')
#converting single image to expected format for model prediction
pred_x = np.expand_dims(vggX_test[30], axis=0)
pred_x.shape
# Use the loaded model to make predictions
#model_joblib.predict(X_test)
Y_pred_test_cls = (model_joblib.predict(pred_x) > 0.5).astype("int32")
plt.figure(figsize=(2,2))
plt.imshow(pred_x.reshape(img_height,img_width,3)/255)
plt.show()
#print('Label - one hot encoded: \n',vggy_test_cat.iloc[30] )
print('Actual Label - one hot encoded: ', vggy_test[30])
print('Predicted Label - one hot encoded: ',Y_pred_test_cls[0] )
one_hot_encoder.inverse_transform(Y_pred_test_cls[0].reshape(1,-1))
# Dumping the transformer to an external pickle file
joblib.dump(one_hot_encoder, 'VGG16_CNN_ohe.pkl')
#Predicting from Pickle file
pkl_model=joblib.load('./Flower_Species_Classifier_VGG16_Augmented_100.pkl')
# Use the loaded model to make predictions
#model_joblib.predict(X_test)
Y_pred_test_cls = (pkl_model.predict(pred_x) > 0.5).astype("int32")
plt.figure(figsize=(2,2))
plt.imshow(pred_x.reshape(img_height,img_width,3)/255)
plt.show()
#print('Label - one hot encoded: \n',vggy_test_cat.iloc[30] )
print('Actual Label - one hot encoded: ', vggy_test[30])
print('Predicted Label - one hot encoded: ',Y_pred_test_cls[0] )
#Loading ppickeled encoder
ohe_pkl=joblib.load('./VGG16_CNN_ohe.pkl')
ohe_pkl.inverse_transform(Y_pred_test_cls[0].reshape(1,-1))
from PIL import ImageTk, Image
import numpy as np
#from tkinter import filedialog
#import tkinter as tk
import tensorflow
from tensorflow.keras.preprocessing.image import img_to_array
from sklearn.externals import joblib
import matplotlib.pyplot as plt
%matplotlib inline
def predict_img(image_data):
#root = tk.Tk()
#image_data = filedialog.askopenfilename(initialdir="/", title="Choose an image",
# filetypes=(("all files", "*.*"), ("jpg files", "*.jpg"), ("png files", "*.png")))
original = Image.open(image_data)
plt.figure(figsize = (5,5))
plt.imshow(original)
original = original.resize((100, 100), Image.ANTIALIAS)
numpy_image = img_to_array(original)
#expanding dimensions as model is expecting a array of image not just a single image
image_batch = np.expand_dims(numpy_image, axis=0)
processed_image=image_batch/255
#Loading Pickled Model , we could have used Keras approach as well but we are going ahead with Pickle for now
vgg_cnn_model =joblib.load('./Flower_Species_Classifier_VGG16_Augmented_100.pkl')
#Loading pickeled encoder for reverse transforming output
ohe_pkl=joblib.load('./VGG16_CNN_ohe.pkl')
#Using the pickeled model for classification
predictions = vgg_cnn_model.predict(processed_image)
#inverse transforming the prediction for folder name details
label = ohe_pkl.inverse_transform(predictions.reshape(1,-1))
print("Predicted label for the Image: ",label)
#root.quit()
#root.destroy()
#test path -->> F:\GreatLearning\AI\ComputerVision\Project\Flowers-Classification\Test\0.jpg
predict_img('F:\\GreatLearning\\AI\\ComputerVision\\Project\\Flowers-Classification\\Test\\0.jpg')
predict_img('F:\\GreatLearning\\AI\\ComputerVision\\Project\\Flowers-Classification\\Test\\1.jpg')
we have to divide the models into two major groups:
Machine Learning Algorithms:
Deep Neural Netowrks ( ANN & CNN ):
Chalanges for Neural Network:
CNN Advantages: